<template>
	<view class="container">
		<st-navigation title="活动详情"></st-navigation>

		<view class="main">
			<view class="sales-promotion-detail">

				<view class="designer">
					<view class="designer-item-info" @click="handleToShop(activityDetail.user_id)">
						<StAvatar class="designer-item-avatar" :designer="designer"></StAvatar>

						<!-- <image class="designer-item-avatar" :src="$wanlshop.ossYSM(designer.avatar, 60, 60, 2, 'avatar')" mode="aspectFill"></image> -->
						<view class="designer-item-name">{{ designer.username || designer.name || designer.nickname }}
						</view>
						<view class="" style="margin-right: 16rpx;margin-left: 8rpx;">
							<view class="d-flex a-center j-center" v-if="designer.is_online===1"
								style="gap: 8rpx; width: 84rpx;height: 36rpx;border-radius: 8rpx;background-color: #E0FAD9;">
								<view class="rounded-circle"
									style="width: 12rpx;height: 12rpx;background-color: #6BC54A;">
								</view>
								<view class="d-flex a-center" style="">
									<text style="color: #76BD53;font-size: 20rpx;">在线</text>
								</view>
							</view>
							<view class="d-flex a-center j-center" v-else
								style="gap: 8rpx; width: 84rpx;height: 36rpx;border-radius: 8rpx;background-color: #E7E7E7;">
								<view class="rounded-circle"
									style="width: 12rpx;height: 12rpx;background-color: #818181;">
								</view>
								<view class="d-flex a-center" style="">
									<text style="color: #818181;font-size: 20rpx;">离线</text>
								</view>
							</view>
						</view>
						<view class="designer-item-desc">进去逛逛>></view>
					</view>
				</view>

				<view class="title" style="color:#000000;font-size: 32rpx;font-weight: 600;">{{ activityDetail.title }}
				</view>

				<view class="tips">活动倒计时: {{ activityDetail.timeStr }}</view>
				<view class="poster">
					<image v-for="(item, index) of activityDetail.desc" :src="item" mode="widthFix"></image>
					<!-- <image :src="activityDetail.img" mode="widthFix"></image> -->
					<view>
						<rich-text :nodes="activityDetail.desc"></rich-text>
					</view>
				</view>
			</view>
		</view>

		<view class="action" style="">
			<view class="tips" style="font-size: 32rpx;">每位用户仅限报名一次</view>
			<view class="btn" @click="handleToYsmFillAppointment">在线报名</view>
		</view>
	</view>
</template>

<script>
	import StAvatar from '../../components/st-avatar/st-avatar.vue';

	export default {
		components: {
			StAvatar
		},
		data() {
			return {
				designer: {
					// name: '张三',
					// avatar: '../../static/images/ysmHome/avatar-1.png',
					// desc: '设计师'
				},

				activityDetail: null,
				timer: null,
			}
		},
		onLoad(option) {
			if (!option.activityDetail) {
				uni.showToast({
					title: '参数错误',
					icon: 'none'
				})
				unit.navigateBack();
				return;
			}


			let activityDetail = JSON.parse(decodeURIComponent((option.activityDetail)));
			this.activityDetail = activityDetail;
			console.log(activityDetail)

			// 查询设计师信息
			this.getDesignerInfo(this.activityDetail.user_id);

			this.renderData();
			this.timer = setInterval(() => {
				this.renderData();
			}, 1000);
		},
		methods: {
			async getDesignerInfo(id) {
				await this.$api.get({
					url: '/wanlshop/user/getDesignerInfo',
					data: {
						type: 'basic',
						id,
					},
					success: res => {
						console.log(res);
						this.designer = res
					}
				});
			},
			handleToShop(id) {
				uni.navigateTo({
					url: `/pagesYSM/ysmDesignerShop/ysmDesignerShop?shop_id=${id}`
				})
			},

			renderData() {
				let countDown = this.countDown(this.activityDetail.endtime);
				if (countDown.day <= '00' && countDown.hour <= '00' && countDown.minute <= '00' && countDown.second <=
					'00') {
					this.activityDetail.timeStr = '活动已结束';
					return;
				}
				let timeStr = `${countDown.day}天 ${countDown.hour}:${countDown.minute}:${countDown.second}`;
				this.activityDetail.timeStr = timeStr;
			},
			// 根据时间戳倒计时
			countDown(time) {
				let now = new Date().getTime();
				let diff = time * 1000 - now;
				let day = Math.floor(diff / (24 * 60 * 60 * 1000));
				let hour = Math.floor((diff % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
				let minute = Math.floor((diff % (60 * 60 * 1000)) / (60 * 1000));
				let second = Math.floor((diff % (60 * 1000)) / 1000);
				return {
					day: day < 10 ? '0' + day : day,
					hour: hour < 10 ? '0' + hour : hour,
					minute: minute < 10 ? '0' + minute : minute,
					second: second < 10 ? '0' + second : second,
				}
			},
			handleToYsmFillAppointment() {
				uni.navigateTo({
					url: `/pagesYSM/ysmDesignerShop/ysmFillAppointment?fromurl_id=${this.activityDetail.id}&fromurl=activity&designer_id=${this.designer.id}&designer=${encodeURIComponent(JSON.stringify(this.designer))}`
				});
			},
		},
		// 销毁
		onUnload() {
			clearInterval(this.timer);
		}
	}
</script>

<style lang="less" scoped>
	.main {
		margin-top: 20rpx;
		padding-bottom: 120rpx;
	}

	.sales-promotion-detail {
		padding: 32rpx;
		background-color: #fff;

		.title {
			font-size: 32rpx;
			font-weight: 500;
			border-top: 1rpx solid #E6E6E6;
			margin-top: 32rpx;
			padding-top: 20rpx;
		}

		.tips {
			font-weight: 400;
			font-size: 24rpx;
			line-height: 34rpx;
			color: #808080;
			margin-top: 16rpx;
			text-align: right;
		}

		.poster {
			margin-top: 32rpx;

			image {
				width: 100%;
			}
		}
	}

	.designer {

		.designer-item-info {
			display: flex;
			flex-direction: row;
			align-items: center;

			/deep/ .designer-item-avatar {
				.designer-avatar {
					width: 140rpx;
					height: 140rpx;

					.designer_level {
						height: 40rpx;
					}
				}
			}

			.designer-item-name {
				font-size: 32rpx;
				color: #333;
				margin-left: 20rpx;
				font-weight: 600;
			}

			.designer-item-desc {
				font-size: 24rpx;
				color: #999;
				// margin-left: 20rpx;
			}
		}
	}

	.action {
		position: fixed;
		bottom: 0;


		left: 0;
		width: 100%;
		// height: 100rpx;
		background-color: #CF402F;
		display: flex;
		align-items: center;
		// justify-content: space-between;
		// padding: 0 32rpx;
		padding-top: 20rpx;
		padding-bottom: env(safe-area-inset-bottom);
		padding-left: 48rpx;
		color: #fff;
		font-size: 24rpx;

		.btn {
			margin-left: 92rpx;
			width: 258rpx;
			height: 80rpx;
			line-height: 80rpx;
			background-color: #F1B61A;
			text-align: center;
			font-size: 32rpx;
			font-weight: 600;
		}
	}
</style>